package cn.icast.netty.c6;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.Random;

@Slf4j
public class SliceClient {
    public static void main(String[] args) {
        send();
    }
    private static void send(){
        NioEventLoopGroup worker=new NioEventLoopGroup();
        try {

            Bootstrap bootstrap=new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.group(worker);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                  ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                      //会在连接成功后触发active
                      @Override
                      public void channelActive(ChannelHandlerContext ctx) throws Exception {
                          ByteBuf buf = ctx.alloc().buffer();
                         /* char c='0';
                              Random r = new Random();
                          for (int i = 0; i < 10; i++) {
                              StringBuilder sb = makeString(c, r.nextInt(256)+1);
                              c++;
                              buf.writeBytes(sb.toString().getBytes());
                          }*/
                          buf.writeBytes("你好".getBytes());
                          ctx.writeAndFlush(buf);

                      }
                  });
                }
            });
            ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("localhost", 8080));
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
           log.error("client error",e);
        } finally {
            worker.shutdownGracefully();
        }
    }


    public static StringBuilder makeString(char c, int len){
        StringBuilder sb=new StringBuilder(len+2);
        for (int i = 0; i < len; i++) {
            sb.append(c);
        }
        sb.append("\n");
        return  sb;
    }
}
